package com.grasp.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.grasp.activity.R;

public class ActionBar extends RelativeLayout {

	private ActionBarOnClick mActionBarOnClick;
	private LayoutInflater mInflater;
	private RelativeLayout mBarView;
	private Button mLeftBtn;
	private Button mRightBtn;
	private TextView mTitleView;

	public ActionBar(Context context) {
		super(context);
	}

	public ActionBar(Context context, AttributeSet attrs) {
		super(context, attrs);

		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (RelativeLayout) mInflater.inflate(R.layout.actionbar, null);
		addView(mBarView);
		mLeftBtn = (Button) mBarView.findViewById(R.id.actionbar_left_btn);
		mRightBtn = (Button) mBarView.findViewById(R.id.actionbar_right_btn);
		mTitleView = (TextView) mBarView.findViewById(R.id.actionbar_title);
		mTitleView.setTextSize(18.0f);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.ActionBar);
		CharSequence title = a.getString(R.styleable.ActionBar_title);
		CharSequence leftTitle = a
				.getString(R.styleable.ActionBar_leftBtnTitle);
		CharSequence rightTitle = a
				.getString(R.styleable.ActionBar_rightBtnTitle);

		int leftBackground = a.getResourceId(R.styleable.ActionBar_leftBackground, -1);
		int rightBackground = a.getResourceId(R.styleable.ActionBar_rightBackground,
				-1);

		boolean leftVisible = a.getBoolean(R.styleable.ActionBar_leftVisible,
				true);
		boolean rightVisible = a.getBoolean(R.styleable.ActionBar_rightVisible,
				true);

		if (!TextUtils.isEmpty(title)) {
			mTitleView.setText(title);
		}
		if (!TextUtils.isEmpty(leftTitle)) {
			mLeftBtn.setText(leftTitle);
		}
		if (!TextUtils.isEmpty(rightTitle)) {
			mRightBtn.setText(rightTitle);
		}
		if(leftBackground != -1){
			mLeftBtn.setBackgroundResource(leftBackground);
		}
		if(rightBackground != -1){
			mRightBtn.setBackgroundResource(rightBackground);
		}
		mLeftBtn.setVisibility(leftVisible ? View.VISIBLE : View.INVISIBLE);
		mRightBtn.setVisibility(rightVisible ? View.VISIBLE : View.INVISIBLE);
		a.recycle();
		mRightBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != mActionBarOnClick) {
					mActionBarOnClick.rightBtnOnClick();
				}
			}
		});
		mLeftBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != mActionBarOnClick) {
					mActionBarOnClick.leftBtnOnClick();
				}
			}
		});
	}

	public void setActionBarOnClickListener(ActionBarOnClick actionBarOnClick) {
		mActionBarOnClick = actionBarOnClick;
	}

	public void setActionBarBackground(int id) {
		mBarView.setBackgroundResource(id);
	}

	public void setActionBarColor(int color) {
		mBarView.setBackgroundColor(color);
	}

	public void setTitle(CharSequence title) {
		mTitleView.setText(title);
	}

	public void setTitle(int resid) {
		mTitleView.setText(resid);
	}

	public void setTitleColor(int color) {
		mTitleView.setTextColor(color);
	}

	public void setLeftBtnText(int id) {
		mLeftBtn.setText(id);
	}

	public void setLeftBtnText(String text) {
		mLeftBtn.setText(text);
	}

	public void setLeftBtnBackground(int id) {
		mLeftBtn.setBackgroundResource(id);
	}

	public void setLeftBtnVisible(boolean visible) {
		mLeftBtn.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
	}

	public void setRightBtnText(int id) {
		mRightBtn.setText(id);
	}

	public void setRightBtnText(String text) {
		mRightBtn.setText(text);
	}

	public void setRightBtnBackground(int id) {
		mRightBtn.setBackgroundResource(id);
	}

	public void setRightBtnVisible(boolean visible) {
		mRightBtn.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
	}

}
